package com.koron.css2.newProjectCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.Enum.PjInfoProcessStateEnum;
import com.koron.css2.newProjectCenter.bean.query.PjTerminationQueryBean;
import com.koron.css2.newProjectCenter.mapper.PjTerminationMapper;
import com.koron.css2.newProjectCenter.vo.PjTerminationListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 工程终止查询列表
 * 1. 前端传query
 * 2. 数据处理
 * 3. 返回列表
 */
public class PjTerminationList implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        try {
            PjTerminationQueryBean pjTerminationQueryBean = JsonUtils.objectToPojo(req.getData(), PjTerminationQueryBean.class);
            PjTerminationMapper pjTerminationMapper = factory.getMapper(PjTerminationMapper.class);
            Map<String, String> PPT = Tools.mapDicByCode(factory, "PPT");
            Map<String, String> TERMINATION_REASON = Tools.mapDicByCode(factory, "PROJECT_TERMINATION_REASON");

            // 1. 设置分页参数
            PageHelper.startPage(pjTerminationQueryBean.getPage(), pjTerminationQueryBean.getPageCount());
            // 2. 查询列表
            List<PjTerminationListVO> list = pjTerminationMapper.selectList(pjTerminationQueryBean);
            for (PjTerminationListVO bean : list) {
                // 3.1 pjType -> pjTypeName
                bean.setPjTypeName(PPT.get(bean.getPjType().toString()));
                // 3.2 processState -> processName
                if (StringUtils.isNotBlank(bean.getProcessState())) {
                    bean.setProcessState(Tools.transProcessStatus(bean.getProcessState()));
                }
                // 3.3 nodeCode -> nodeCodeName
                String nodeCode = bean.getNodeCode();
                if (StringUtils.isNotEmpty(nodeCode)) {
                    String nodeCodeName = PjInfoProcessStateEnum.getNodeCodeName(nodeCode);
                    bean.setNodeCodeName(nodeCodeName);
                }
                // 3.4 terminationReason -> terminationReasonName
                if (bean.getTerminationReason() != null) {
                    bean.setTerminationReasonName(TERMINATION_REASON.get(bean.getTerminationReason().toString()));
                }
            }
            // 4. 返回结果
            MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "工程终止列表查询成功", PageInfo.class);
            info.setData(new PageInfo<>(list));
            return info;
        } catch (Exception e) {
            logger.error("工程终止列表查询失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程终止列表查询失败", null);
        }
    }

}
